define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'account/index' + location.search,
                    add_url: 'account/add',
                    edit_url: 'account/edit',
                    del_url: 'account/del',
                    multi_url: 'account/multi',
                    import_url: 'account/import',
                    table: 'device_account',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='groupIds']", form).addClass("selectpage").data("source", "auth/group/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                sortName: 'device.mobile_serial',
                sortOrder: 'asc',
                fixedRightNumber: 1,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'groupIds', title: __('分组'), visible: false},
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('Admin_id')},
                        {
                            field: 'device.mobile_serial', 
                            title: __('Device.mobile_serial'),
                            formatter: function(value, row, index) {
                                let color_class = '';
                                if(row.status == 0){
                                    color_class = 'text-purple';
                                }else if(row.status == 2){
                                    color_class = 'text-red';
                                }

                                if(row.status != 1){
                                    return '<b class="'+ color_class + '">' + value + '</b>'
                                }

                                return '<a href="javascript:;" data-id="'+ row.id +'" title="添加明细" class="detail' + color_class + '" data-area=\'["50%", "80%"]\'><b>' + value + '</b></a>'
                            }
                        },
                        {field: 'device.remark', title: __('Device.remark'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'register_name', title: __('Register_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'country', title: __('Country'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'device.ip', title: __('Ip'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'ban_day', title: __('Ban_day')},
                        {field: 'is_open', title: __('Is_open'), searchList: {"0":__('Is_open 0'),"1":__('Is_open 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.off('dbl-click-row.bs.table');

            $(document).on("click", ".detail", function () {
                let account_id = $(this).data('id');
                layer.confirm('请先选择养号或者带货',{
                    title: '添加明细',
                    btn: ['养号', '带货']
                }, function(){
			        Backend.api.open('collect/add?type=1&account_id=' + account_id, __('添加'),{area:['50%', '80%']});
                }, function(){
			        Backend.api.open('collect/add?type=2&account_id=' + account_id, __('添加'),{area:['50%', '80%']});
                })
			});
        },
        
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'account/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'account/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'account/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on('click', 'input[name="row[status]"]:checked', function(){
                let status = $(this).val();
                if(status != 0){
                    $('.ban').addClass('hide');
                }else{
                    $('.ban').removeClass('hide');
                }
            })

            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on('click', 'input[name="row[status]"]:checked', function(){
                let status = $(this).val();
                if(status != 0){
                    $('.ban').addClass('hide');
                }else{
                    $('.ban').removeClass('hide');
                }
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
